﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NewCash2DeviceServer
{
    class LogHelperNew
    {
        public string logFile = "";
        // log文件路径         
        /// <summary>        
        /// /// 默认log文件       
        /// /// </summary>       
        string today = "[yyyy - MM - dd]";
        public LogHelperNew()
        {
            string logFile = AppDomain.CurrentDomain.BaseDirectory + "/log/" + DateTime.Now.ToString(today)+"log.txt";
            Init(logFile);
        }
        /// <summary>        /// 指定名称，创建文件        /// </summary>      
       
        public LogHelperNew(string LogName)
        {
            string logFile = AppDomain.CurrentDomain.BaseDirectory + "/log/" + LogName + ".txt";
            if (LogName.Contains("csv"))
            {
                logFile = AppDomain.CurrentDomain.BaseDirectory + "/log/" + LogName ;
            }
            Console.WriteLine("logFile" + logFile);
            Init(logFile);
        }
        public LogHelperNew(string LogName,int id)
        {
            string logFile = AppDomain.CurrentDomain.BaseDirectory + "/" + LogName ;
            Init(logFile);
        }

        /// <summary>        /// 初始设置，创建路径、文件        /// </summary>        
        private void Init(string logFile)
        {
            this.logFile = logFile;             // 创建路径            
            String dir = System.IO.Path.GetDirectoryName(logFile);
            Console.WriteLine("路径为" + dir);
            if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
            // 创建文件            
            if (!File.Exists(logFile))
            {
                FileStream fs = File.Create(logFile);
                fs.Close();
            }
        }
        public void deleteTxt()
        {
            string logPath = AppDomain.CurrentDomain.BaseDirectory + "/log/";
            DirectoryInfo folder = new DirectoryInfo(logPath);
            foreach (FileInfo file in folder.GetFiles("*.txt"))
            {
                DateTime dt = file.CreationTime;
                if (dt < DateTime.Today.AddDays(-7))
                {
                    try
                    {
                        File.Delete(file.FullName);
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }


        }
        string FORMAT = "[yyyy-MM-dd_HH:mm:ss_ffff] ";         
        /// <summary>        /// 追加一条log信息        /// </summary>        
        public void Write(string text)
        {
            using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8))
            {
                sw.Write(DateTime.Now.ToString(FORMAT) + text);
            }
        }       
        /// <summary>        /// 追加一行log信息        /// </summary>        
        public void WriteLine(string text)
        {
            text += "\r\n";
            using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8))
            {
                sw.Write(DateTime.Now.ToString(FORMAT) + text);
            }
        }
        public void WriteClear()
        {
            FileStream stream = File.Open(logFile, FileMode.OpenOrCreate, FileAccess.Write);
            stream.Seek(0, SeekOrigin.Begin);
            stream.SetLength(0);
            stream.Close();
        }
        public void WriteLineNoTime(string text)
        {
            text += "\r\n";
           
            using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8))
            {
                sw.Write(text);
            }
        }
        public  void WriteMessage( string message)
        {
            using (FileStream fs = File.Open(logFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {

                Byte[] info = new UTF8Encoding(true).GetBytes(message);
                fs.Write(info, 0, info.Length);
            }
        }
        public  string ReadMessage()
        {

            StreamReader srReadFile = new StreamReader(logFile);
            string strReadLine = "";
            // 读取流直至文件末尾结束
            while (!srReadFile.EndOfStream)
            {
                strReadLine = srReadFile.ReadLine(); //读取每行数据

            }
            // 关闭读取流文件
            srReadFile.Close();
            return strReadLine;
        }
        public static string ReadMessageByPath(string path)
        {

            StreamReader srReadFile = new StreamReader(path);
            string strReadLine = "";
            // 读取流直至文件末尾结束
            while (!srReadFile.EndOfStream)
            {
                strReadLine = srReadFile.ReadLine(); //读取每行数据

            }
            // 关闭读取流文件
            srReadFile.Close();
          
            return strReadLine;
        }
        public static string ReadByPath(string path)
        {

            StreamReader srReadFile = new StreamReader(path);
            string strReadLine = "";
            // 读取流直至文件末尾结束
            while (!srReadFile.EndOfStream)
            {
                strReadLine += srReadFile.ReadLine()+"\r\n"; //读取每行数据
                
            }
            // 关闭读取流文件
            srReadFile.Close();

            return strReadLine;
        }

    }
}
